var express = require('express');
const fs = require('fs');
const path = require('path');
var router = express.Router();
// 返回格式
const BizResult = require("../config/BizResult");
// 请求路由
const CategoryModel = require('../models/CategoryModel');

/* 重置类目 */
router.get('/addCategory', function (req, res, next) {
  const { categoryList } = require("../public/json/category.json");
  CategoryModel.insertMany(categoryList).then(() => {
    res.send(BizResult.success('添加类目成功'));
  }).catch(() => {
    res.send(BizResult.fail('添加类目失败'));
  })
});

/* 查询类目 */
router.get('/getCategory', function (req, res, next) {
  let { categoryType } = req.query;
  let dataObj = {};
  if (categoryType) dataObj.$or = [
    { categoryType },
    { categoryType: 3 }
  ]
  CategoryModel.find(dataObj).then((data, err) => {
    if (err) {
      res.send(BizResult.fail(err))
      return false;
    }
    res.send(BizResult.success(data));
  })
});



module.exports = router;